/********************************************************************************
* Copyright (c) 2009, Matteo Pelati (matteo.pelati@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Matteo Pelati ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************/

#include "stdafx.h"
#include <iostream>
#include <exception>


#include "DbXmlException.h"
#include "DbXmlValue.h"
#include "DbXmlManager.h"
#include "DbXmlQueryExpression.h"
#include "DbXmlModify.h"
#include "DbXmlUpdateContext.h"



namespace BerkeleyDB { namespace Xml
{

	IEnumerable<DbXmlValue^>^ DbXmlValue::SelectNodes(String^ query)
	{
		try
		{
			if (this->currCtx == nullptr) 
				throw new XmlException(XmlException::NULL_POINTER, "No Query Context is associated to the current XmlValue ");

			CLRSTR2STDSTR(cquery, query);
			XmlManager* man = ((DbXmlManager^)Manager)->GetInnerObject();
			XmlQueryExpression exp = man->prepare(cquery, *(currCtx->GetInnerObject()));
			return (IEnumerable<DbXmlValue^>^) gcnew DbXmlValueCollection(this, exp.execute(*(this->GetInnerObject()), *(currCtx->GetInnerObject()), DB_READ_COMMITTED), currTxn, currCtx); 
		}
		rethrowDbXmlException();	
	}

	DbXmlValue^ DbXmlValue::SelectNode(String^ query)
	{
		XmlValue val;
		try
		{	
			if (this->currCtx == nullptr) 
				throw new XmlException(XmlException::NULL_POINTER, "No Query Context is associated to the current XmlValue ");

			XmlResults res;
			CLRSTR2STDSTR(cquery, query);
			XmlManager* man = ((DbXmlManager^)Manager)->GetInnerObject();
			XmlQueryContext ctx = man->createQueryContext(XmlQueryContext::LiveValues, XmlQueryContext::Lazy); 
			XmlQueryExpression exp = man->prepare(cquery, ctx);
			res = exp.execute(*(this->GetInnerObject()), ctx, DB_READ_COMMITTED); 

			res.reset();
			if (res.next(val)) return gcnew DbXmlValue(this, val, currTxn, currCtx);
			else return nullptr;
		}
		rethrowDbXmlException();	
	}

	bool DbXmlValue::HasAttribute(String^ name)
	{
		DbXmlValue^ val = SelectNode("./@" + name);
		if (val == nullptr) return false;
		else return true;
	}

	String^ DbXmlValue::GetAttribute(String^ name)
	{
		DbXmlValue^ val = SelectNode("./@" + name);
		return ((val == nullptr) ? nullptr : val->asString());
	}

	/*
	DbXmlValueCollection^ DbXmlValue::Get_Attributes()
	{
		try
		{
			return gcnew DbXmlValueCollection(this, n_val->getAttributes());
		}
		rethrowDbXmlException();		
	}
	*/

}}